<template>
  <div class="model-form">
    <el-page-header @back="goBack" content="增加客户">
    </el-page-header>
    <div class="model-content">
      <el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="客户名称" prop="name">
              <el-input v-model="form.name" name="name">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="完整手机号" prop="phone">
              <el-input v-model="form.phone" name="phone">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="用户标签" prop="customerLabel">
              <el-select style="width: 100%;" v-model="form.customerLabel" filterable clearable
                         placeholder="请选择">
                <el-option v-for="item in customerLabelOptions" :key="item.id"
                           :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="标签转化时间" prop="labelUpdatedDate">
              <el-date-picker v-model="form.labelUpdatedDate" type="date" value-format="YYYY-MM-DD"
                              placeholder="选择日期">
              </el-date-picker>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="商户" prop="shop">
              <el-select style="width: 100%;" v-model="form.shop" filterable clearable
                         placeholder="请选择">
                <el-option v-for="item in shopOptions" :key="item.id"
                           :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="分配商户" prop="assignedShop">
              <el-select style="width: 100%;" v-model="form.assignedShop" filterable clearable
                         placeholder="请选择">
                <el-option v-for="item in assignedShopOptions" :key="item.id"
                           :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="分组" prop="customerGroup">
              <el-select style="width: 100%;" v-model="form.customerGroup" filterable clearable
                         placeholder="请选择">
                <el-option v-for="item in customerGroupOptions" :key="item.id"
                           :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="缴费来源" prop="fromFee">
              <el-input v-model="form.fromFee" name="fromFee">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="推广人名称" prop="channelName">
              <el-input v-model="form.channelName" name="channelName">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="推广人ID" prop="channelId">
              <el-input v-model="form.channelId" name="channelId">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="企业渠道ID" prop="enterpriseChannel">
              <el-input v-model="form.enterpriseChannel" name="enterpriseChannel">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="comment">
              <el-input v-model="form.comment" name="comment">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="换电次数" prop="swapTotal">
              <el-input v-model="form.swapTotal" name="swapTotal">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="首次取电时间" prop="firstRentDate">
              <el-date-picker v-model="form.firstRentDate" type="date" value-format="YYYY-MM-DD" placeholder="选择日期">
              </el-date-picker>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="到期时间" prop="expiredDate">
              <el-date-picker v-model="form.expiredDate" type="date" value-format="YYYY-MM-DD" placeholder="选择日期">
              </el-date-picker>
            </el-form-item>

          </el-col>

          <el-col :span="22">
            <el-form-item>
              <el-button @click="goBack">取消</el-button>
              <el-button type="primary" @click="add">确定</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import common from "@/mixins/common.js";
import {ElLoading} from "element-plus";

import nbSelect from "@/components/nbSelect.vue";

export default {
  name: "Customer_add",
  mixins: [common],
  components: {nbSelect},
  data() {
    return {
      form: {
        name: '',
        phone: '',
        customerLabel: '',
        labelUpdatedDate: '',
        shop: '',
        assignedShop: '',
        customerGroup: '',
        fromFee: '',
        channelName: '',
        channelId: '',
        enterpriseChannel: '',
        comment: '',
        swapTotal: '',
        firstRentDate: '',
        expiredDate: ''
      },
      customerLabelOptions: [],
      shopOptions: [],
      assignedShopOptions: [],
      customerGroupOptions: [],
      rules: {
        name: [
          {required: true, message: '请输入客户名称', trigger: 'blur'}
        ],
        shop: [
          {required: true, message: '请输入商户', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    async add() {
      try {
        let valid = await this.$refs["ruleForm"].validate();
        if (!valid) {
          return;
        }
      } catch (e) {
        return;
      }

      await this.addData();
    },
    async addData() {
      let data = this.form;
      const loading = ElLoading.service({
        lock: true,
        text: '数据处理中',
        background: 'rgba(0, 0, 0, 0.7)',
      })

      let res = await this.$http.form("/customer/create", data);

      loading.close();

      if (res.code !== 200) {
        this.$message.error(res.msg);
        return;
      }

      this.$message({
        message: '添加数据成功',
        type: 'success'
      });
      this.$router.go(-1);
    },
    async loadCustomerLabelOptions() {
      let self = this;
      let param = {};
      param.sortMethod = "asc";
      param.sortField = "id";
      param.level = 1;
      param.size = 500;
      param.fetch = 0;
      let res = await this.$http.form("/customerLabel/list", param);
      if (res.code === 200) {
        self.customerLabelOptions = res.data;
      }
    },
    async loadShopOptions() {
      let self = this;
      let param = {};
      param.sortMethod = "asc";
      param.sortField = "id";
      param.level = 1;
      param.size = 500;
      param.fetch = 0;
      let res = await this.$http.form("/shop/list", param);
      if (res.code === 200) {
        self.shopOptions = res.data;
      }
    },
    async loadAssignedShopOptions() {
      let self = this;
      let param = {};
      param.sortMethod = "asc";
      param.sortField = "id";
      param.level = 1;
      param.size = 500;
      param.fetch = 0;
      let res = await this.$http.form("/shop/list", param);
      if (res.code === 200) {
        self.assignedShopOptions = res.data;
      }
    },
    async loadCustomerGroupOptions() {
      let self = this;
      let param = {};
      param.sortMethod = "asc";
      param.sortField = "id";
      param.level = 1;
      param.size = 500;
      param.fetch = 0;
      let res = await this.$http.form("/customerGroup/list", param);
      if (res.code === 200) {
        self.customerGroupOptions = res.data;
      }
    },

  },
  mounted() {
    this.loadCustomerLabelOptions();
    this.loadShopOptions();
    this.loadAssignedShopOptions();
    this.loadCustomerGroupOptions();
  }
}

</script>

<style scoped>
</style>
